package com.xm.xmapp.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.xm.xmapp.R;

public class CustomRatingBar extends LinearLayout {
	ImageView[] img = new ImageView[5];

	public CustomRatingBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		View view = LayoutInflater.from(context).inflate(R.layout.view_customratingbar, this, true);
		img[0] = (ImageView) view.findViewById(R.id.one);
		img[1] = (ImageView) view.findViewById(R.id.two);
		img[2] = (ImageView) view.findViewById(R.id.three);
		img[3] = (ImageView) view.findViewById(R.id.four);
		img[4] = (ImageView) view.findViewById(R.id.five);
		initEvent();
	}

	private void initEvent() {
		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.one:
					setRatingBar(1);
					break;
				case R.id.two:
					setRatingBar(2);
					break;
				case R.id.three:
					setRatingBar(3);
					break;
				case R.id.four:
					setRatingBar(4);
					break;
				case R.id.five:
					setRatingBar(5);
					break;
				default:
					break;
				}

			}
		};
		img[0].setOnClickListener(listener);
		img[1].setOnClickListener(listener);
		img[2].setOnClickListener(listener);
		img[3].setOnClickListener(listener);
		img[4].setOnClickListener(listener);
		setRatingBar(4);
	}

	int score;

	public void setRatingBar(int score) {
		if (this.score != score) {
			this.score = score;
			int size = img.length;
			for (int i = 0; i < size; i++) {
				if (i < score) {
					img[i].setImageResource(R.mipmap.order_evaluate_choose);
				} else {
					img[i].setImageResource(R.mipmap.order_evaluate_nochoose);
				}
			}
		}
	}
//
//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		int x = (int) event.getX();
//		int with = getWidth();
//		int score;
//		switch (event.getAction()) {
//		case MotionEvent.ACTION_DOWN:
//			return true;
//		case MotionEvent.ACTION_MOVE:
//			x = (int) event.getX();
//			with = getWidth();
//			if (x <= 0) {
//				score = 1;
//			} else if (x >= with) {
//				score = 5;
//			} else {
//				score = with / (with - x);
//			}
//			setRatingBar(score);
//			return true;
//		case MotionEvent.ACTION_CANCEL:
//		case MotionEvent.ACTION_UP:
//			x = (int) event.getX();
//			with = getWidth();
//			if (x <= 0) {
//				score = 1;
//			} else if (x >= with) {
//				score = 5;
//			} else {
//				score = with / (with - x);
//			}
//			setRatingBar(score);
//			return true;
//
//		default:
//			break;
//		}
//		return super.onTouchEvent(event);
//	}

}
